package org.example.service;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.example.bean.Position;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("positionService")
public class PositionService {
    @Autowired
    private SolrClient solrClient;

    // 添加数据
    public   void  addPosition(Position position){
        try {
            solrClient.addBean(position);
            solrClient.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }

    public List<Position> queryPositions(String query){
        SolrQuery solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.set("df","companyName");
        solrQuery.setQuery(query);
        // 高亮显示设置
        solrQuery.setHighlight(true);
        // 添加高亮显示的域
        solrQuery.addHighlightField("companyName positionName");
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
        try {
            QueryResponse queryResponse = solrClient.query(solrQuery);
            if (queryResponse == null){
                return  null;
            }
            // 获取列表
//            List<Position>  positionList = queryResponse.getBeans(Position.class);
            SolrDocumentList docs = queryResponse.getResults();
            // 如果少于5条重新查找
            if (docs.getNumFound() < 5) {
                System.out.println(">>>>============= Original query has less than 5 result");
                solrQuery.setQuery(query + " OR positionAdvantage:美女多||员工福利好");
                queryResponse = solrClient.query(solrQuery);
                docs = queryResponse.getResults();
            }

            // 获取高亮
            Map<String, Map<String,List<String>>> hilightMap = queryResponse.getHighlighting();

            List<Position> positionList = assemblePositions(docs);
            // 赋值高亮信息
            for (Position position : positionList){
                List<String> list = hilightMap.get(position.getId()).get("companyName");
                if(!CollectionUtils.isEmpty(list)){
                    position.setCompanyName(list.get(0));
                }
                list = hilightMap.get(position.getId()).get("positionName");
                if(!CollectionUtils.isEmpty(list)){
                    position.setPositionName(list.get(0));
                }
            }
            return  positionList;
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }

    public List<Position> assemblePositions(SolrDocumentList docs) {
        List<Position>  positionList = new ArrayList<>();
        for (SolrDocument doc : docs) {
            Position position = new Position();
            String companyName = (String) doc.getFieldValue("companyName");
            if (companyName != null) {
                position.setCompanyName(companyName);
            }
            String id = (String) doc.getFieldValue("id");
            if (companyName != null) {
                position.setId(id);
            }
            String positionName = (String) doc.getFieldValue("positionName");
            if (positionName != null) {
                position.setPositionName(positionName);
            }
            String positionAdvantage = (String) doc.getFieldValue("positionAdvantage");
            if (positionAdvantage != null) {
                position.setPositionAdvantage(positionAdvantage);
            }
            String salary = (String) doc.getFieldValue("salary");
            if (salary != null) {
                position.setCompanyName(salary);
            }

            positionList.add(position);
        }

        return positionList;
    }

}
